#define _CRT_SECURE_NO_WARNINGS		
#include<stdio.h>
 
 struct Node {
   int val;
   struct Node *next;
   struct Node *random;
 };
 

struct Node* copyRandomList(struct Node* head) {

    if (head == NULL)
    {

        return NULL;
    }struct Node* cur = head;
    while (cur)
    {
        struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
        struct Node* next = cur->next;
        copy->random = NULL;
        copy->next = NULL;
        copy->next = next;
        copy->val = cur->val;
        cur->next = copy;
        cur = next;

    }

    cur = head;

    while (cur)
    {
        struct Node* copy = cur->next;
        if (cur->random)
            copy->random = cur->random->next;
        else
            copy->random = NULL;
        cur = cur->next->next;
    }
    cur = head;
    struct Node* newcopy = head->next;
    while (cur)
    {
        struct Node* copy = cur->next;
        struct Node* next = copy->next;
        cur->next = copy->next;
        if (next)
            copy->next = next->next;
        else
        {
            copy->next = NULL;
        }

        cur = next;

    }

    return newcopy;


}

struct ListNode {
    int val;
    struct ListNode* next;
    
};






typedef struct ListNode node;
struct ListNode* insertionSortList(struct ListNode* head) {
    if (head == NULL || head->next == NULL)
    {
        return head;

    }

    node* newhead = head;
    node* cur = head->next;
    newhead->next = NULL;
    while (cur)
    {
        node* next = cur->next;
        node* newprev = NULL;
        node* newcur = NULL;
        if (cur->val <= newhead->val)
        {
            cur->next = newhead;
            newhead = cur;
        }
        else
        {
            newprev = newhead;
            newcur = newhead->next;
            while (newcur)
            {
                if (cur->val > newcur->val)
                {
                    newprev = newcur;
                    newcur = newcur->next;
                }
                else if (cur->val <= newcur->val)
                {
                    cur->next = newcur;
                    newprev->next = cur;
                    break;
                }

            }
            if (newcur == NULL)
            {
                newprev->next = cur;
                cur->next = NULL;
            }

        }
        cur = next;

    }
    return newhead;
}